package com.zqq.goods.ui.fragment

import android.os.Bundle
import android.support.v7.widget.GridLayoutManager
import android.support.v7.widget.LinearLayoutManager
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.alibaba.android.arouter.launcher.ARouter
import com.kennyc.view.MultiStateView
import com.zhuanju.core.utils.ZLog
import com.zqq.baselibrary.ext.setVisible
import com.zqq.baselibrary.ext.startLoading
import com.zqq.baselibrary.ui.adapter.BaseRecyclerViewAdapter
import com.zqq.baselibrary.ui.fragment.BaseMvpFragment
import com.zqq.goods.R
import com.zqq.goods.injection.component.DaggerCategoryComponent
import com.zqq.goods.injection.module.CategoryModule
import com.zqq.goods.presenter.CategoryPresenter
import com.zqq.goods.presenter.view.CategoryView
import com.zqq.goods.service.bean.Category
import com.zqq.goods.ui.adapter.SecondCategoryAdapter
import com.zqq.goods.ui.adapter.TopCategoryAdapter
import com.zqq.provider.router.RouterPath
import kotlinx.android.synthetic.main.fragment_category.*

/**
 * 二、商品分类 Fragment
 * wc
 * @author zqq on 2018/8/1
 */
class CategoryFragment : BaseMvpFragment<CategoryPresenter>(), CategoryView {

    lateinit var topAdapter: TopCategoryAdapter //一级分类Adapter
    lateinit var secondAdapter: SecondCategoryAdapter //二级分类Adapter


    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        super.onCreateView(inflater, container, savedInstanceState)
        return inflater.inflate(R.layout.fragment_category, container, false)
    }

    override fun injectComponent() {
        DaggerCategoryComponent.builder()
                .activityComponent(mActivityComponent)
                .categoryModule(CategoryModule())
                .build()
                .inject(this)
        mPresenter.mView = this
    }

    override fun onStart() {
        super.onStart()
        ZLog.e("--onstart--")
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        ZLog.e("onActivityCreated")
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        ZLog.e("onViewCreated")
        initView()
        loadData()
    }

    //初始化视图
    private fun initView() {
        //一级菜单
        mTopCategoryRv.layoutManager = LinearLayoutManager(context)
        topAdapter = TopCategoryAdapter(context!!)
        mTopCategoryRv.adapter = topAdapter

        topAdapter.setOnItemClickListener(object : BaseRecyclerViewAdapter.OnItemClickListener<Category> {
            override fun onItemClick(item: Category, position: Int) {
                for (category in topAdapter.dataList) {
                    category.isSelected = item.id == category.id
                }
                topAdapter.notifyDataSetChanged()
                loadData(item.id)
            }
        })

        //二级菜单
        mSecondCategoryRv.layoutManager = GridLayoutManager(context, 3)
        secondAdapter = SecondCategoryAdapter(context!!)
        mSecondCategoryRv.adapter = secondAdapter
        secondAdapter.setOnItemClickListener(object : BaseRecyclerViewAdapter.OnItemClickListener<Category> {
            override fun onItemClick(item: Category, position: Int) {
                ARouter.getInstance()
                        .build(RouterPath.GoodsCenter.PATH_GOODS)
                        .withInt(RouterPath.GoodsCenter.KEY_CATEGORY_ID, item.id)
                        .navigation()
            }
        })
    }

    //加载数据
    private fun loadData(parentId: Int = 0) {
        if (parentId != 0) {
            mMultiStateView.startLoading()
        }
        mPresenter.getCategory(parentId)
    }


    /**------------------接口返回-------------------*/
    //获取商品分类 回调
    override fun onGetCategoryResult(result: MutableList<Category>?) {
        if (result != null && result.size > 0) {
            if (result[0].parentId == 0) {
                result[0].isSelected = true
                topAdapter.setData(result)
                mPresenter.getCategory(result[0].id)
            } else {
                secondAdapter.setData(result)
                mTopCategoryIv.setVisible(true)
                mCategoryTitleTv.setVisible(true)

                //多试图
                mMultiStateView.viewState = MultiStateView.VIEW_STATE_CONTENT
            }
        } else {
            //没有数据
            mTopCategoryIv.setVisible(false)
            mCategoryTitleTv.setVisible(false)

            //多视图
            mMultiStateView.viewState = MultiStateView.VIEW_STATE_EMPTY
        }
    }

}